<template>
    <!-- 电脑登录框-->
    <div class="body">
        <div class="shell">
            <!-- 第一部分 -->
            <div class="container a-container" id="a-container">
                <form action="" method="" class="form" id="a-form">
                    <h2 class="form_title title">你好</h2>
                    <h2 class="form_title title1">欢迎来到我的博客</h2>
                    <div class="form_icons flex justify-center item-center" @click="goIndex">
                        <img src="@/assets/logo.png" class="iconfont w-3/12" />
                    </div>
                    <span class="form_span text-lg">{{ shici.content }}</span>
                    <ul class="flex space-x-2  items-center h-8">
                        <li v-for=" tag  in  shici.matchTags " class="border-2 border-current rounded-full">
                            {{ tag }}</li>
                    </ul>
                </form>
            </div>
            <div class="container b-container" id="b-container">
                <el-form class="w-5/6 md:w-2/5 space-y-4 form" ref="formRef" :rules="rules" :model="form" id="b-form">
                    <h2 class="form_title title">登入账号</h2>
                    <div class="form_icons flex justify-center item-center" @click="goIndex">
                        <img src="@/assets/logo.png" class="iconfont w-1/5" />
                    </div>
                    <span class="form_span">欢迎回来</span>
                    <el-form-item prop="username">
                        <el-input type="text" v-model="form.username" placeholder="用户名" :prefix-icon="User" clearable />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" v-model="form.password" placeholder="密码" :prefix-icon="Lock" show-password
                            clearable />
                    </el-form-item>
                    <el-button class="form_button button submit" :loading="loading" type="primary" @click="onSubmit">
                        登 录</el-button>
                </el-form>
            </div>
            <!-- 第二部分 -->
            <div class="switch" id="switch-cnt">
                <div class="switch_circle"></div>
                <div class="switch_circle switch_circle-t"></div>
                <div class="switch_container" id="switch-c1">
                    <h2 class="switch_title title" style="letter-spacing: 0;">Welcome Back！</h2>
                    <p class="switch_description description">肉体苦难，机械飞升</p>
                    <p class="switch_description description">一起加入光荣的进化吧！！！</p>
                    <button class="switch_button button switch-btn" @click="changeForm">SIGN IN</button>
                </div>

                <div class="switch_container is-hidden " id="switch-c2">
                    <h2 class="switch_title title" style="letter-spacing: 0;">Hello Friend！</h2>
                    <p class="switch_description description">如果你没有账号，可以点击网站图标返回首页</p>
                    <p class="switch_description description"></p>
                    <button class="switch_button button switch-btn" @click="changeForm">LAST PAGE</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
// 图案引入
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/admin/user'
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
// 路由
import { useRouter } from 'vue-router';
// 消息组件
import { showMessage } from '@/composables/util'
import { setToken } from '@/composables/cookie'
import { useUserStore } from '@/stores/user'
import { load } from 'jinrishici'



// 登录按钮加载
const loading = ref(false)

//用户信息
const userStore = useUserStore()

let switchCtn = ref(null)
let switchC1 = ref(null)
let switchC2 = ref(null)
let switchCircle = ref(null)
let aContainer = ref(null)
let bContainer = ref(null)

onMounted(() => {
    document.addEventListener('keyup', onKeyUp)

    switchCtn = document.querySelector("#switch-cnt");
    switchC1 = document.querySelector("#switch-c1");
    switchC2 = document.querySelector("#switch-c2");
    switchCircle = document.querySelectorAll(".switch_circle");
    aContainer = document.querySelector("#a-container");
    bContainer = document.querySelector("#b-container");

    loadSentence()


});

//获取诗词
let shici = ref(
    {
        content: "",
        matchTags: []
    }
)
const loadSentence = () => {
    load(result => {
        shici.value = result.data
        console.log(result.data)
        console.log(shici.value.content)
        console.log(shici.value.matchTags)
    },
        err => {
            console.log("诗词错误");
        })
}

const changeForm = (e) => {
    console.log("开始切换")
    // 修改类名
    switchCtn.classList.add("is-gx");
    setTimeout(() => {
        switchCtn.classList.remove("is-gx");
    }, 1500)
    switchCtn.classList.toggle("is-txr");
    switchCircle[0].classList.toggle("is-txr");
    switchCircle[1].classList.toggle("is-txr");

    switchC1.classList.toggle("is-hidden");
    switchC2.classList.toggle("is-hidden");
    aContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-z");
}




//表单登录处理
// 定义响应式的表单对象
const form = reactive({
    username: '',
    password: ''
})

// 登录
const router = useRouter()

// 表单引用
const formRef = ref(null)

// 表单验证规则
const rules = {
    username: [
        {
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
        }
    ],
    password: [
        {
            required: true,
            message: '密码不能为空',
            trigger: 'blur',
        },
    ]
}

const onSubmit = () => {
    console.log('登录')
    // 先验证 form 表单字段
    formRef.value.validate((valid) => {
        if (!valid) {
            console.log('表单验证不通过')
            return false
        }

        // 开始加载
        loading.value = true

        // 调用登录接口
        login(form.username, form.password).then((res) => {
            console.log(res)
            // 判断是否成功
            if (res.success == true) {
                // 存储 Token 到 Cookie 中
                let token = res.data.token
                setToken(token)
                // 提示登录成功
                showMessage(form.username + '，欢迎回来')

                // 获取用户信息，并存储到全局状态中
                userStore.setUserInfo()

                // 跳转到后台首页
                router.push('/admin/index')
            } else {
                let message = res.message
                // 提示消息
                showMessage(message, 'error')
            }
        })
            .finally(() => {
                // 结束加载
                loading.value = false
            })

    })
}

// 按回车键后，执行登录事件
function onKeyUp(e) {
    console.log(e)
    if (e.key == 'Enter') {
        onSubmit()
    }
}

// 移除键盘监听
onBeforeUnmount(() => {
    document.removeEventListener('keyup', onKeyUp)
})

const goIndex = () => {
    console.log('返回')
    router.push('/')
}

</script>

<style scoped>
* {
    font-family: '思源黑体 Normal', Helvetica, 'Microsoft YaHei', Arial, sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* 字体无法选中 */
    user-select: none;
}


.body {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    background-color: #ecf0f3;
    color: #a0a5a8;
}

.shell {
    position: relative;
    width: 1000px;
    min-width: 1000px;
    min-height: 600px;
    height: 600px;
    padding: 25px;
    background-color: #ecf0f3;
    box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
    border-radius: 12px;
    overflow: hidden;
}

/* 设置响应式 */
@media (max-width: 1200px) {
    .shell {
        transform: scale(0.7);
    }
}

@media (max-width: 1000px) {
    .shell {
        transform: scale(0.6);
    }
}

@media (max-width: 800px) {
    .shell {
        transform: scale(0.5);
    }
}

@media (max-width: 600px) {
    .shell {
        transform: scale(0.4);
    }
}

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    width: 600px;
    height: 100%;
    padding: 25px;
    background-color: #ecf0f3;
    transition: 1.25s;
}

.form {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.iconfont {
    margin: 0 5px;
    /* border: rgba(0, 0, 0, 0.5) 2px solid;
    border-radius: 20%; */
    font-size: 50px;
    padding: 3px;
    opacity: 1;
    transition: 0.15s;

}

.iconfont:hover {
    opacity: 0.5;
    transition: 0.1s;
    cursor: pointer;
}

/* 输入框样式 */
::v-deep(.el-input__wrapper) {
    width: 350px;
    height: 40px;
    margin: 4px 0;
    padding-left: 25px;
    font-size: 13px;
    letter-spacing: 0.15px;
    border: none;
    outline: none;
    background-color: #ecf0f3;
    transition: 0.25s ease;
    border-radius: 8px;
    box-shadow: inset 2px 2px 4px #d1d9e6,
        inset -2px -2px 4px #f9f9f9;
}

::v-deep(.el-input) {
    --el-input-placeholder-color: #769bf0;
    /* 聚焦时边框样式 */
    --el-input-hover-border-color: #d0191900;
    --el-input-clear-hover-color: #769bf0;
    --el-input-focus-border-color: #f3f2f200;
}

::v-deep(.el-input__wrapper:hover) {
    box-shadow: -2px -2px 8px rgba(255, 255, 255, 1),
        -2px -2px 12px rgba(255, 255, 255, 0.5),
        inset 2px 2px 4px rgba(255, 255, 255, 0.1),
        inset 2px 2px 8px rgba(0, 0, 0, 0.15);
}

::v-deep(.el-input__wrapper:focus-within) {
    box-shadow: -2px -2px 8px rgba(255, 255, 255, 1),
        -2px -2px 12px rgba(255, 255, 255, 0.5),
        inset 2px 2px 4px rgba(255, 255, 255, 0.1),
        inset 2px 2px 8px rgba(0, 0, 0, 0.15);
}

/* 警告时输入框样式 */
::v-deep(.el-form-item.is-error .el-input__wrapper) {
    box-shadow:
        inset 2px 2px 4px #d1d9e6,
        inset -2px -2px 4px #f9f9f9,
        -2px -2px 8px rgba(255, 255, 255, 1),
        -2px -2px 12px rgba(255, 255, 255, 0.5),
        inset 2px 2px 4px rgba(255, 255, 255, 0.1),
        2px 2px 8px rgba(0, 0, 0, 0.15);
}

/* 输入框文字警告颜色 */
::v-deep(.el-form-item__error) {
    color: #769bf0;
}

/* 输入框警告点击 */
::v-deep(.el-form-item.is-error .el-input__wrapper.is-focus) {
    box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9 !important;
}

/* 输入框警告悬停 */
::v-deep(.el-form-item.is-error .el-input__wrapper:hover) {
    box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
}

.form_span {
    margin-top: 30px;
    margin-bottom: 12px;
}

.form_link {
    color: #181818;
    font-size: 15px;
    margin-top: 25px;
    border-bottom: 1px solid #a0a5a8;
    line-height: 2;
}

.title {
    font-size: 34px;
    font-weight: 700;
    line-height: 3;
    color: #181818;
    letter-spacing: 10px;
}

.title1 {
    margin-top: -50px;
    font-size: 34px;
    font-weight: 700;
    line-height: 3;
    color: #181818;
    letter-spacing: 10px;
}


.description {
    font-size: 14px;
    letter-spacing: 0.25px;
    text-align: center;
    line-height: 1.6;
}

.button {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 1.15px;
    background-color: #4B70E2;
    color: #f9f9f9;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
    border: none;
    outline: none;
}

.a-container {
    z-index: 100;
    left: calc(100% - 600px);
}

.b-container {
    left: calc(100% - 600px);
    z-index: 0;
}

.switch {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 400px;
    padding: 50px;
    z-index: 200;
    transition: 1.25s;
    background-color: #ecf0f3;
    overflow: hidden;
    box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #d1d9e6;
}

.switch_circle {
    position: absolute;
    width: 500px;
    height: 500px;
    border-radius: 50%;
    background-color: #ecf0f3;
    box-shadow: inset 8px 8px 12px #b8bec7, inset -8px -8px 12px #fff;
    bottom: -60%;
    left: -60%;
    transition: 1.25s;
}

.switch_circle-t {
    top: -30%;
    left: 60%;
    width: 300px;
    height: 300px;
}

.switch_container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    position: absolute;
    width: 400px;
    padding: 50px 55px;
    transition: 1.25s;
}

.switch_button {
    cursor: pointer;
}

.switch_button:hover,
.submit:hover {
    box-shadow: 6px 6px 10px #d1d9e6, -6px -6px 10px #f9f9f9;
    transform: scale(0.985);
    transition: 0.25s;
}

.switch_button:active,
.switch_button:focus {
    box-shadow: 2px 2px 6px #d1d9e6, -2px -2px 6px #f9f9f9;
    transform: scale(0.97);
    transition: 0.25s;
}

.is-txr {
    left: calc(100% - 400px);
    transition: 1.25s;
    transform-origin: left;
}

.is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
}

.is-z {
    z-index: 200;
    transition: 1.25s;
}

.is-hidden {
    visibility: hidden;
    opacity: 0;
    position: absolute;
    transition: 1.25s;
}

.is-gx {
    animation: is-gx 1.25s;
}

@keyframes is-gx {

    0%,
    10%,
    100% {
        width: 400px;
    }

    30%,
    50% {
        width: 500px;
    }
}


::v-deep(.el-button--primary) {
    --el-button-hover-bg-color: rgba(75, 112, 226);
}
</style>